package com.itzz.spring_zhoneHeng.controller;

import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.UserDao;
import com.itzz.spring_zhoneHeng.pojo.User;
import com.itzz.spring_zhoneHeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
//
    @Autowired
    UserDao userDao;

    @Autowired
    UserService userService;

    //使用apifoxs查询所有的角色
    @RequestMapping("/foxsAll")
    public RestResponse foxsAll(User user){
        RestResponse response = userService.apifoxsAll(user);
        return response;
    }

    //查询所有的用户和它的角色
    @RequestMapping("/queryAll")
    public RestResponse queryAll(User user, Integer pageNum, Integer pageSize){
        RestResponse response = userService.queryAll(user, pageNum, pageSize);

        return response;
    }


    //根据用户id回显用户信息和角色信息
    @RequestMapping("/queryById/{id}")
    public RestResponse queryById(@PathVariable("id") Integer id){
        RestResponse response = userService.queryById(id);

        return response;
    }

    //修改用户信息
    @RequestMapping("/edit")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse edit(User user, Integer[] rids, MultipartFile files){
        RestResponse response = userService.edit(user, rids,files);

        return response;
    }

    //修改用户的个人信息
    @RequestMapping("/editOwn")
    public RestResponse editUserOwn(User user, Integer[] rids, MultipartFile files){
        RestResponse response = userService.edit(user, rids,files);

        return response;
    }

//    根据uid删除用户
    @RequestMapping("/del/{id}")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse del(@PathVariable("id") Integer id){
        RestResponse response = userService.del(id);
        return response;
    }


    //根据用户的姓名查询用户个人信息   userDao   queryUser
    @RequestMapping("/queryUser")
    public RestResponse queryUser(String username){
        User user = userDao.queryUser(username);
        return RestResponse.ok(user);
    }
//    @RequestMapping("/level")
//    public RestResponse level(Integer id){
//        //查询每一个用户的等级
//        String level = userDao.getLevel(id);
//        return RestResponse.ok(level);
//    }

    //添加用户
    @RequestMapping("/addUser")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse addUser(User user,Integer[] rids,Integer mbid,MultipartFile files){
//        System.out.println(user);
//        System.out.println(rids);
//        System.out.println(uid);
//        System.out.println(mbid);
        RestResponse response = userService.addUser(user,rids,mbid,files);
        return response;
    }


    @RequestMapping("/queryAllMoney")
    public RestResponse queryAllMoney(User user, Integer pageNum, Integer pageSize){

        RestResponse response = userService.queryAll(user, pageNum, pageSize);

        return response;
    }

    //修改余额
    @RequestMapping("/editMoney")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse editMoney(User user){
        int i = userDao.updateById(user);

        return RestResponse.ok(i);
    }

    //充值根据用户名查询余额  AddMoney
    @RequestMapping("/AddMoney")
    public RestResponse queryMoney(String username,String money){
        if(username==null||money==null){
            return RestResponse.fail(400,"参数错误");
        }
        Double aDouble = Double.valueOf(money);
        userDao.AddMoney(username,aDouble);
        return RestResponse.ok();
    }

    //根据用户id查询用户的角色
    @RequestMapping("/getRoleName/{id}")
    public RestResponse getRole(@PathVariable("id") Integer id){
        String roleName = userDao.queryRoleName(id);
        if(roleName!=null){
            return RestResponse.ok(roleName);
        }
        return RestResponse.fail(400,"查询失败");
    }


    //根据userid查询u_ms表  中的messid
    //根据messid 查询所有的消息记录
    @RequestMapping("/queryMessId/{id}")
    public RestResponse queryMessId(@PathVariable("id") Integer id){
        RestResponse response = userService.queryMessId(id);
        return response;
    }






}
